local yaohu = fk.CreateSkill{
    name = "wk_heg__yaohu",
}

Fk:loadTranslationTable{
  ["wk_heg__yaohu"] = "邀虎",
  [":wk_heg__yaohu"] = "准备阶段，你可<a href=':DoGiveDeputy'>遣调</a>另一张已明置的武将牌于一名与你势力不同的角色至其回合结束，然后你视为对其使用【远交近攻】且其可视为对你使用【过河拆桥】。",

  ["#wk_heg__yaohu_choose"] = "邀虎：选择一名与你势力不同的角色，遣调另一张武将牌于其并视为对其使用【远交近攻】",
  ["#wk_heg__yaohu-discard"] = "邀虎：你可以视为对 刘璋 使用一张【过河拆桥】",

  ["$wk_heg__yaohu1"] = "",
  ["$wk_heg__yaohu2"] = "",
}

local H = require "packages/ol_hegemony/util"
local HG = require "packages/new_heg_diy_my/util"

yaohu:addEffect(fk.EventPhaseStart,{
    anim_type = "special",
    can_trigger = function(self, event, target, player, data)
      return player:hasSkill(yaohu.name) and player == target and target.phase == Player.Start
       and ((player.general == "wk_heg__liuzhang" and not player.deputyGeneral:startsWith("blank_") and player.deputyGeneral ~= "anjiang")
        or (player.deputyGeneral == "wk_heg__liuzhang" and not player.general:startsWith("blank_") and player.general ~= "anjiang" and not string.find(player.general, "lord")))
    end,
    on_cost = function(self, event, target, player, data)
      local room = player.room
      local targets = table.filter(room.alive_players, function (p) return H.compareKingdomWith(p, player, true) and p:getMark("@wk_give_deputy") == 0 end)
      if #targets > 0 then
        local tos = room:askToChoosePlayers(player,{
          targets = targets,
          min_num = 1,
          max_num = 1,
          skill_name = yaohu.name,
          prompt = "#wk_heg__yaohu_choose",
        })
        if #tos > 0 then
          event:setCostData(self, {tos = tos[1]})
          return true
        end
      end
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local to = event:getCostData(self).tos
      if player.dead or to.dead then return end
      local isDeputy = H.inGeneralSkills(player, yaohu.name)
      if isDeputy then
        local choice = player.general
        if player.general == "wk_heg__liuzhang" then
            choice = player.deputyGeneral
        end
        HG.DoGiveDeputy(room, player, to, choice, player.kingdom, isDeputy ~= "d")
        room:setPlayerMark(to, "wk_heg__yaohu_back", choice)
      end
      room:useVirtualCard("befriend_attacking", nil, player, to, yaohu.name, true)
      if room:askToSkillInvoke(to, {skill_name = yaohu.name, prompt = "#wk_heg__yaohu-discard"}) then
        room:useVirtualCard("dismantlement", nil, to, player, yaohu.name, true)
      end
    end,
})

yaohu:addEffect(fk.TurnEnd,{
    is_delay_effect = true,
    anim_type = "special",
    can_trigger = function(self, event, target, player, data)
      return player.room.current == player and player:getMark("@wk_give_deputy") == player:getMark("wk_heg__yaohu_back") 
       and player:getMark("wk_heg__yaohu_back") ~= 0
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
      HG.StopGiveDeputy(player.room, player, yaohu.name)
      player.room:setPlayerMark(player, "wk_heg__yaohu_back", 0)
    end,
})

return yaohu